package org.popupcommunication.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;

import org.popupcommunication.service.MessageManager;
import org.springframework.beans.factory.annotation.Autowired;


public abstract class Email {
	@Autowired
	static MessageManager messageService;
	static String username = null;
	static String password = null;
	
	public static Session getSessionMail(ServletContext ctx) throws IOException{
		Properties prop = new Properties();	
		InputStream input = null;	
		//load datas from gmail.properties
		input = new FileInputStream(ctx.getRealPath("/WEB-INF/gmail.properties"));			
		prop = new Properties();
		prop.load(input);	
		username = prop.getProperty("gmail.account");
		password = prop.getProperty("gmail.password");
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", prop.getProperty("mail.smtp.auth"));
		props.put("mail.smtp.starttls.enable", prop.getProperty("mail.smtp.starttls.enable"));
		props.put("mail.smtp.host", prop.getProperty("mail.smtp.host"));
		props.put("mail.smtp.port", prop.getProperty("mail.smtp.port"));
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
		return session;
	}
	
	public static void sendAdminEmail(ServletContext ctx,String subject, String xmsg) throws UnsupportedEncodingException{
		String sujet = null,msg = "",recipient="";
		Session session = null;
		
		msg = xmsg;
		sujet = subject;
		try {
			session = getSessionMail(ctx);
			recipient = username;
		} catch (IOException e) {
			e.printStackTrace();
		}

//		Enumeration<?> e = prop.propertyNames();
//		while (e.hasMoreElements()) {
//			String key = (String) e.nextElement();
//			String value = prop.getProperty(key);
//			System.out.println("Key : " + key + ", Value : " + value);
//		}		
		//work email 
		try {
 
			Message message = new MimeMessage(session);
			message.setHeader("Content-Type", "text/plain; charset=UTF-8");
			message.setHeader("Content-Encoding","UTF-8");
			message.setFrom(new InternetAddress(username));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(recipient));
			message.setSubject(sujet);
			message.setContent(new String(msg.getBytes("UTF-8"), "UTF-8"), "text/html");
 
			Transport.send(message);
 
		} catch (MessagingException er) {
			throw new RuntimeException(er);
		}		
	}
	
	public static void sendUserEmail(ServletContext ctx,String subject, String xmsg,String email) throws IOException{
		String sujet = null,msg = "",recipient="";
		Session session = null;
		
		msg = xmsg;
		sujet = subject;
		recipient = email;
		try {
			session = getSessionMail(ctx);
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
 
			Message message = new MimeMessage(session);
			message.setHeader("Content-Type", "text/plain; charset=UTF-8");
			message.setHeader("Content-Encoding","UTF-8");
			message.setFrom(new InternetAddress(username));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(recipient));
			message.setSubject(sujet);
			message.setContent(new String(msg.getBytes("UTF-8"), "UTF-8"), "text/html");
 
			Transport.send(message);
 
		} catch (MessagingException er) {
			throw new RuntimeException(er);
		}			
	}
}
